package com.gok.pay.service.impl;

import com.gok.model.pay.PayOrder;
import com.gok.pay.service.ITestService;
import com.gok.persistence.service.IBaseService;
import com.sy.shiro.UsernameToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by zhuwenquan on 2017/12/27 0027.
 */
@Service
@Slf4j
public class TestService implements ITestService {
    @Resource
    private IBaseService baseService;

    @Override
    public List<PayOrder> selectPayOrder() {
        PayOrder order = new PayOrder();
        order.setOrderNo(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        order.setOrderStatus("New");
        return baseService.listQuery("from PayOrder where 1=1", PayOrder.class);
    }

    @Override
    @Transactional
    public PayOrder createPayOrder() {
        PayOrder order = new PayOrder();
        order.setOrderNo(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        order.setOrderStatus("New");
        baseService.create(order);
        return order;
    }

    @Override
    @Transactional
    public void updatePayOrder(PayOrder order) {
        order.setOrderStatus("Active");
        baseService.update(order);
    }

    /**
     *
     * @return
     */
    @Override
    public UsernameToken getUser() {
        // 获取用户信息
        Subject subject = SecurityUtils.getSubject();
        UsernameToken token = new UsernameToken();


        PrincipalCollection pr = subject.getPrincipals();
        if( pr != null )
        {
            Object o1 = pr.getPrimaryPrincipal();
            token = (UsernameToken)subject.getPrincipals().getPrimaryPrincipal();
            return token;
        }else {
            return null;
        }
    }
}
